package com.py.credit.controller;

import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.LhjcDynamicMapNum;
import com.py.credit.service.LhjcDynamicMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/lhjcDynamicMap")
public class LhjcDynamicMapController {

    @Autowired
    private LhjcDynamicMapService lhjcDynamicMapService;

    /**
     * 中间主体类型分布
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/ztTypeDistribution.json")
    public RestServiceResult getZtTypeDistribution() {
        Map<String, Object> map = new HashMap<String, Object>();
        int jiMessNum = lhjcDynamicMapService.selectMessNum("J", "", "", "");
        int cjMessNum = lhjcDynamicMapService.selectMessNum("C", "", "", "");
        int frMessNum = lhjcDynamicMapService.selectMessNum("", "C", "", "");
        int glcfMessNum = lhjcDynamicMapService.selectMessNum("", "", "Y", "");
        int zrrMessNum = lhjcDynamicMapService.selectMessNum("", "P", "", "");
        int jStatusMessNum = lhjcDynamicMapService.selectMessNum("", "", "", "J");
        int gStatusMessNum = lhjcDynamicMapService.selectMessNum("", "", "", "G");
        int oStatusMessNum = lhjcDynamicMapService.selectMessNum("", "", "", "O");

        List<LhjcDynamicMapNum> bmFqNum = lhjcDynamicMapService.selectBmFqNum();
        List<LhjcDynamicMapNum> bmFeedbackNum = lhjcDynamicMapService.selectBmFeedbackNum();

        map.put("jiMessNum", jiMessNum);
        map.put("cjMessNum", cjMessNum);
        map.put("frMessNum", frMessNum);
        map.put("glcfMessNum", glcfMessNum);
        map.put("zrrMessNum", zrrMessNum);
        map.put("jStatusMessNum", jStatusMessNum);
        map.put("gStatusMessNum", gStatusMessNum);
        map.put("oStatusMessNum", oStatusMessNum);
        map.put("bmFqNum", bmFqNum);
        map.put("bmFeedbackNum", bmFeedbackNum);
        return new RestServiceResult(map);
    }

    /**
     * 左右第一个联合奖惩规则
     */
    @ResponseBody
    @RequestMapping(value = "/gzNum.json")
    public RestServiceResult getGzNum(@RequestParam(defaultValue = "", required = true, value = "jclb") String jclb) {
        Map<String, Object> map = new HashMap<String, Object>();
        int provinceNum = lhjcDynamicMapService.selectMessTypeNum(jclb, "W");
        int areaNum = lhjcDynamicMapService.selectMessTypeNum(jclb, "B");

        List<LhjcDynamicMapNum> messGzNameNum = lhjcDynamicMapService.selectMessGzName(jclb);

        map.put("provinceNum", provinceNum);
        map.put("areaNum", areaNum);
        map.put("messGzNameNum", messGzNameNum);
        return new RestServiceResult(map);
    }

    /**
     * 左右第二个联合奖惩事件主体
     *
     * @param jclb 奖惩类别：C:惩戒 J:激励
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/ztNum.json")
    public RestServiceResult getZtNum(@RequestParam(defaultValue = "", required = true, value = "jclb") String jclb) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        int frMessNum = lhjcDynamicMapService.selectMessNum(jclb, "C", "", "");
        int zrrMessNum = lhjcDynamicMapService.selectMessNum(jclb, "P", "", "");

        List<LhjcDynamicMapNum> frMonthMessNum = lhjcDynamicMapService.selectMonthMessNum(jclb, "C");
        List<LhjcDynamicMapNum> zrrMonthMessNum = lhjcDynamicMapService.selectMonthMessNum(jclb, "P");

        map.put("frMessNum", frMessNum);
        map.put("zrrMessNum", zrrMessNum);
        map.put("frMonthMessNum", frMonthMessNum);
        map.put("zrrMonthMessNum", zrrMonthMessNum);
        map.put("startTime", lhjcDynamicMapService.selectMinTime() == null ? sdf.format(date) : lhjcDynamicMapService.selectMinTime());
        map.put("endTime", lhjcDynamicMapService.selectMaxTime() == null ? sdf.format(date) : lhjcDynamicMapService.selectMaxTime());
        return new RestServiceResult(map);
    }

    /**
     * 左右第三个联合奖惩事件发起
     *
     * @param jclb 奖惩类别：C:惩戒 J:激励
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/fqNum.json")
    public RestServiceResult getFqNum(@RequestParam(defaultValue = "", required = true, value = "jclb") String jclb) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> deptNumMap = new HashMap<String, Object>();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String startTime = "";
        String endTime = sdf.format(date);

        String[] split = endTime.split("-");
        int year = Integer.parseInt(split[0]);
        int month = Integer.parseInt(split[1]);

        if (month == 12) {
            month = 1;
        } else {
            year = year - 1;
            month = month + 1;
        }

        if (month < 10) {
            startTime = year + "-0" + month;
        } else {
            startTime = year + "-" + month;
        }

        List<Map<String, String>> deptIdList = lhjcDynamicMapService.selectMonthBmFqId(jclb);

        for (int i = 0; i < deptIdList.size(); i++) {
            deptNumMap.put(deptIdList.get(i).get("deptName"), lhjcDynamicMapService.selectMonthBmFqNum(jclb, Integer.parseInt(deptIdList.get(i).get("id")), startTime, endTime));
        }

        map.put("deptNum", deptNumMap);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        return new RestServiceResult(map);
    }

    /**
     * 左右第四个联合奖惩事件参与
     *
     * @param jclb 奖惩类别：C:惩戒 J:激励
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/cyNum.json")
    public RestServiceResult getCyNum(@RequestParam(defaultValue = "", required = true, value = "jclb") String jclb) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> dateNumMap = new HashMap<String, Object>();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String startTime = "";
        String endTime = sdf.format(date);

        String[] split = sdf.format(date).split("-");
        int year = Integer.parseInt(split[0]);
        int month = Integer.parseInt(split[1]);

        if (month == 12) {
            month = 1;
        } else {
            year = year - 1;
            month = month + 1;
        }

        if (month < 10) {
            startTime = year + "-0" + month;
        } else {
            startTime = year + "-" + month;
        }

        year = Integer.parseInt(split[0]);
        month = Integer.parseInt(split[1]);

        for (int i = 0; i < 12; i++) {
            if (month == 0) {
                year = year - 1;
                month = 12;
            }

            if (month < 10) {
                dateNumMap.put(year + "-0" + month, lhjcDynamicMapService.selectMonthBmCyNum(jclb, year + "-0" + month));
            } else {
                dateNumMap.put(year + "-" + month, lhjcDynamicMapService.selectMonthBmCyNum(jclb, year + "-" + month));
            }

            month--;
        }

        map.put("dateNum", dateNumMap);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        return new RestServiceResult(map);
    }

    /**
     * 左右第五个联合奖惩事件规则分布
     *
     * @param jclb 奖惩类别：C:惩戒 J:激励
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/gzDistribution.json")
    public RestServiceResult getGzDistribution(@RequestParam(defaultValue = "", required = true, value = "jclb") String jclb) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<LhjcDynamicMapNum> messGzDistributionNum = lhjcDynamicMapService.selectMessGzDistributionNum(jclb);
        map.put("messGzDistributionNum", messGzDistributionNum);
        return new RestServiceResult(map);
    }
}
